package com.app.service;

import com.app.model.main.Article;
import com.app.model.main.RewardArticle;
import com.app.model.main.UserInfo;
import com.app.model.response.RetCode;
import com.app.model.response.RetStatus;
import com.app.repository.interfaces.ArticleMapper;
import com.app.repository.interfaces.RewardArticleMapper;
import com.app.repository.interfaces.UserInfoMapper;
import com.app.support.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by Lichaojie on 2017/3/11. It's a beautiful day.
 */
@Service
public class TestService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private RewardArticleMapper rewardArticleMapper;

    @Cacheable(value = "test-key",keyGenerator = "wiselyKeyGenerator")
    public void set(){
        RetStatus retStatus = new RetStatus();
        retStatus.setCode(100)
                .setMessage(RetCode.getMessage(100))
                .setBody(new String("test body!"));
        redisUtil.set("key1", retStatus);
        //String str = (String)redisUtil.get("key1");
    }

    public RetStatus get(){
        RetStatus result;
        if(redisUtil.exists("key1")){
            result = (RetStatus)redisUtil.get("key1");
        }else{
            result = new RetStatus();
        }
        return result;
    }

    @Transactional(propagation= Propagation.REQUIRED,rollbackFor = Exception.class,readOnly = false)
    public RetStatus test() throws Exception{
        try {
            Article article = new Article();
            article.setUserId((long) 141)
                    .setContent("fuck")
                    .setFlag((byte) 1)
                    .setImage("jjj")
                    .setLabel("dddd")
                    .setTime("123456")
                    .setTitle("fuck")
                    .setType((byte) 2);
            articleMapper.insert(article);
            RewardArticle rewardArticle = new RewardArticle();
            /*rewardArticle.setStatus((byte)0)
                    .setArticleId((long)999)
                    .setFlag((byte)1)
                    .setCommentId((long)888)
                    .setCoin((long)444)
                    .setAdoptedTime("888")
                    .setTime("0000")
                    .setUserId((long)77);*/
            if(rewardArticleMapper.insert(rewardArticle)!=1)
                throw new Exception();
        }catch(Exception e){
            throw e;
        }finally {
            return new RetStatus().setCode(200).setMessage("置顶帖数量已满");
        }
    }

}
